﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OntoWPFSilverlight
{
    public class CustomSlider : Slider
    {
        public double TickFrequency { get; set; }

        //public string[] Testo { get; set; }

        public Canvas bottomTicksCanvas;

        public DataTemplate TickTemplate
        {
            get { return (DataTemplate)GetValue(TickTemplateProperty); }
            set { SetValue(TickTemplateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TickTemplate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TickTemplateProperty =
            DependencyProperty.Register("TickTemplate", typeof(DataTemplate), typeof(CustomSlider), null);


        public CustomSlider()
        {
            bottomTicksCanvas = new Canvas();
            DefaultStyleKey = typeof(CustomSlider);
            //SizeChanged += new SizeChangedEventHandler(CustomSlider_SizeChanged);
            //Build();

        }

        
       
        //void CustomSlider_SizeChanged(object sender, SizeChangedEventArgs e)
        public void Create_Ticks(string [] testo)
        {
            //bottomTicksCanvas.Children.Clear();
            int numberOfTicks = (int)((Maximum - Minimum+1) / TickFrequency);
            //numberOfTicks++;
            for (int i = 0; i < numberOfTicks; i++)
            {               
                double x1= 5 + ((i) * ((this.ActualWidth - 10) / numberOfTicks));              

                bottomTicksCanvas.Children.Add(CreateTick(new Point(x1, 0), new Point(x1, 5)));              
                //bottomTicksCanvas.Children.Add(CreateLabel((Math.Round(((Maximum - Minimum) / numberOfTicks) * (i), 0)).ToString(), 6.0, x1));                                     
                bottomTicksCanvas.Children.Add(CreateLabel(testo[i],6.0,x1));
            }
        }

        TextBlock CreateLabel(string text,double top, double left)
        {
            TextBlock txt = new TextBlock();
            txt.Text = text;
            txt.SetValue(Canvas.TopProperty, top);
            txt.SetValue(Canvas.LeftProperty, left - txt.ActualWidth / 2);
            return txt;
        }

        FrameworkElement CreateTick(Point start, Point end)
        {            
            if (TickTemplate == null)
            {
                Line ln = new Line();
                ln.Stroke = new SolidColorBrush(Colors.Black);
                ln.StrokeThickness = 1.0;
                ln.X1 = start.X;
                ln.Y1 = start.Y;
                ln.X2 = end.X;
                ln.Y2 = end.Y;
                return ln;
            }
            else
            {
                ContentPresenter cp = new ContentPresenter();
                cp.Content = "a";
                cp.ContentTemplate = TickTemplate;
                cp.SetValue(Canvas.TopProperty, start.Y);
                cp.SetValue(Canvas.LeftProperty, start.X - (cp.ActualWidth/2));
                return cp;
            }
            
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            bottomTicksCanvas = GetTemplateChild("BottomTicksCanvas") as Canvas;
                   
        }
    }
}
